package BackTrackingPackage3;

import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Lzm
 * @version 1.0
 */
public class letterCombinations_ {
  String[] letterMap =
    new String[]
      {
        "",  //0
        "",  //1
      "abc", //2
      "def", //3
      "ghi", //4
      "jkl", //5
      "mno", //6
      "pqrs",//7
      "tuv", //8
      "wxyz" //9
      };
  List<String> result = new ArrayList<>();
  List<String> path = new ArrayList<>();
  StringBuilder sb = new StringBuilder();
  @Test
  public void letterCombinationsTest(){
    List<String> strings = letterCombinations("23");
    System.out.println(strings.toString());
  }
  public List<String> letterCombinations(String digits) {
    if (digits.length() == 0){
      return result;
    }
    backTracking(digits,0);
    return result;
  }

  // 与众不同, 其他都是从一个集合中组合, 这是从俩集合中组合
  public void backTracking(String digits,int startIndex){
    if (startIndex == digits.length()){
      result.add(sb.toString());
      return;
    }
      String s = letterMap[Integer.parseInt(digits.charAt(startIndex) + "")];
      for (int i = 0 ; i < s.length() ; i ++){
        sb.append(s.charAt(i) + "");
        backTracking(digits, startIndex + 1);
        sb.deleteCharAt(sb.length() - 1);
      }
  }
}
